<?php
class InventoryAction extends CommonAction{
	
	public function _initialize() {
		
		parent::_initialize();
		//parent::checkMBAC();
		
	}
	public function inbound(){
		$flag = $_POST['flag'];//获取标志位
		if(IS_POST){
			$inventory = M("Inventory");
			//查询操作
			if($flag=="search"){
				$data = $_POST['serchcontent'];
				if($data==null){
					$this->error("搜索条件不能为空");
				}else{
					$sql = "select * from et_erweima where code='".$data."' or name like '%".$data."%'";
					$model = new Model();
					$values = $model->query($sql);
					$this->assign('value',$values);
				}
			}
			//添加操作
			if($flag=="add"){
				$inventory = D("Inventory");
				$stockin = M("Stockin");//商品入库单
				$proinout = M('Proinout');//商品出入库记录
				$id = $_POST['id'];//商品编号
				$num = $_POST['num'];//数量
				$min_num = $_POST['min_num'];//商品报警量
				$purprice = $_POST['purprice'];//单品进货价格
				$data1['purprice'] = $purprice;
				$data1['num'] = $num;
				$data1['totalprice'] = $num*$purprice;
				$data1['time'] = time();
				$data1['id'] = $id;
				$stockin->add($data1);
				$si_id = $stockin->where($data1['time'])->getField("si_id");
				$in_id = $inventory->where($data1['id'])->getField("in_id");
				$data2['time'] = time();
				$data2['si_id'] = $si_id;
				$data2['type'] = "入库";
				$data2['in_id'] = $in_id;
				$proinout->add($data2);
				if($inventory->create()){
					$data['num'] = $num;
					$data['min_num'] = $_POST['min_num'];
					$data['id'] = $id;
					$data['st_id'] = $_POST['st_id'];
					$com = $inventory->where("id=".$id)->getField('id');
					if($com==""){
						$inventory->add($data);
					}else{
						$inve = $inventory->where("id=".$id)->find();
						$new['num'] = $inve['num'] + $num;
						$new['min_num'] = $min_num;
						$inventory->where("id=".$id)->save($new);
						$this->success("修改成功");
					}
				}else{
					$this->error("该选项不能为空！");
				}
				$this->success("入库成功！");
			}
			
		}else{
			$flag = $_GET['flag'];//flag标识
			if($flag==1){
				$id = $_GET['id'];
				$inventory = M("Inventory");
				$inv = $inventory->where("id=".$id)->find();
				$erweima = M('Erweima');
				$erv = $erweima->find($id);
				if($inv!=null){
					$stockin = M("Stockin");//商品入库单
					$st = $stockin->where('id='.$inv['id'])->order('time DESC')->find();
					$this->assign('st',$st);
					$this->assign('erv',$erv);
					$this->assign('inv',$inv);
				}else{
					$this->assign('erv',$erv);
				}
			}
			
		}
		$this->display();
	}
	public function outbound(){
		if(IS_POST){
			$flag = $_POST['flag'];
			if($flag=="tijiao"){
				$code = $_POST['product']; 
				$out_num = $_POST['out_num'];
				$erweima = D("Erweima");//商品表
				$stockin = M("Stockout");//商品入库单
				$proinout = M('Proinout');//商品出入库记录
				$inventory = M('Inventory');//库存表 
				$id = $erweima->where("code=".$code)->getField("id");
				
				
			}
			if($flag=="ptijiao"){
				
			}	
		}else{
			
		}
		$this->display();
	}
}